package com.sen.result;

import lombok.Data;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * 统一返回的结果类
 */
@Data
public class Result implements Serializable {

    private static final long serialVersionUID = 1L;

    private int code;
    private String msg;
    private Map<String, Object> data = new HashMap<>();

    public Result(int code, String msg){
        this.code = code;
        this.msg = msg;
    }

    public Result(ResultEnum resultEnum){
        this.code = resultEnum.getCode();
        this.msg = resultEnum.getMsg();
    }

    public static Result ok(){
        return new Result(ResultEnum.SUCCESS);
    }

    public static Result error(){
        return new Result(ResultEnum.ERROR);
    }

    public Result data(String k, Object v){
         this.data.put(k, v);
         return this;
    }

    public Result code(int code){
        this.code = code;
        return this;
    }

    public Result msg(String msg){
        this.msg = msg;
        return this;
    }

    @Override
    public String toString() {
        return "Result{" +
                "code=" + code +
                ", msg='" + msg + '\'' +
                ", data=" + data +
                '}';
    }
}
